/// <summary>
/// Scenario Framework WAPI Activities
/// </summary>
/// <author>Maurizio Farina (maurizio.farina@gmail.com)</author>
using System;
using System.Runtime.InteropServices;
using Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;

namespace Scenario.WAPI
{
	/// <summary>
	/// WAPIActivity.
	/// </summary>
    public partial class ActivityHelper
	{
        public enum ActivityOperation
        {
            Acquire,
            Assign,
            ReAssign,
            Lock,
            CompleteAsynch,
            Complete,
            Stop,
            Release
        }

        public enum ActivityList
        {
            LockedByUser,
            AvailableByUser,
            VisibileByUser,
            Notifications,
            All
        }

		public ActivityHelper()
		{
        }

        #region Get/Set Attributes
        public XMLAttributes SetAttributes(string processID, String activityName, XMLAttributes xmlAttributes, string sUser)
        {
            Response wapiResponse = new Response();
            XMLActivity xmlActivity = new XMLActivity();
            xmlActivity.ProcessId = processID;
            xmlActivity.Name = activityName;

            if (xmlAttributes.Count == 0)
                return null;

            String sResult = ActivityHelper.SetActivityAttributes(xmlActivity.GetXML(false, 0), xmlAttributes.GetXML(false,0), sUser, ref wapiResponse);
            if (wapiResponse.iCode != 0)
            {
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
            }
            if ((sResult != null) && (sResult.CompareTo(String.Empty) != 0))
            {
                XMLAttributes xmlAttributesNotSetted = new XMLAttributes();
                xmlAttributesNotSetted.Parse(sResult);

                return xmlAttributesNotSetted;
            }
            return null;
        }

        public XMLAttributes GetAttributes(string processID, String activityName, string user)
        {
            Response wapiResponse = new Response();
            XMLActivity xmlActivity = new XMLActivity();
            xmlActivity.ProcessId = processID;
            xmlActivity.Name = activityName;

            String xml = ActivityHelper.GetActivityAttributes(xmlActivity.GetXML(false, 0), user, ref wapiResponse);

            if (wapiResponse.iCode != 0)
            {
                 throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
            }
            XMLAttributes attributes = new XMLAttributes();
            attributes.Parse(xml);

            return attributes;

        }

        #endregion

        #region Activity List

        //TODO: E' un vero casino
        public XMLActivities GetActivities(ActivityList listType, String processID, String user)
        {
            WAPI.Response wapiResponse = new WAPI.Response();

            String xml = String.Empty;

            //search all activity
            if ((processID == null) || (processID.Length == 0))
            {
                switch ( listType ) 
                {
                    case ActivityList.AvailableByUser:
                        xml = ActivityHelper.GetActivityList(user, ref wapiResponse);
                        break;

                    case ActivityList.All:
                        xml = ActivityHelper.GetAllActivityList(user, ref wapiResponse);
                        break;

                    case ActivityList.VisibileByUser:
                        xml = ActivityHelper.GetAllMyActivityList(user, ref wapiResponse);
                        break;

                    case ActivityList.LockedByUser:
                        xml = ActivityHelper.GetWorkItemListRunningNoNotify(user, ref wapiResponse);
                        break;
                    case ActivityList.Notifications:
                        xml = ActivityHelper.GetWorkItemListByNotify(user, ref wapiResponse);
                        break;
                }

                /*
                xml = ActivityHelper.GetWorkItemList(user, ref wapiResponse);
                xml = ActivityHelper.GetWorkItemListNoRunning(user, ref wapiResponse);
                xml = ActivityHelper.GetWorkItemListRunning(user, ref wapiResponse);
                xml = ActivityHelper.GetWorkItemListCompleted(user, ref wapiResponse);
                */
            }
            else
            {
                if ((user == null) || (user.Length == 0))
                    xml = ActivityHelper.GetActivitiesByProcess(processID, ref wapiResponse);
                else
                {
                    xml = ActivityHelper.GetActivityListByProcess(processID, user, ref wapiResponse);
                    xml = ActivityHelper.GetWorkItemListNoRunningByProcess(processID, user, ref wapiResponse);
                    xml = ActivityHelper.GetWorkItemListRunningByProcess(processID, user, ref wapiResponse);
                }
            }

            XMLActivities activities = new XMLActivities();
            activities.Parse(xml);

            return activities;
        }

        #endregion

        #region Acquire, Assign, ReAssign, Lock, Complete, Release, Stop

        public void PlayActivity(String processId, String activityName, String user, ActivityOperation operation, String resultCode)
        {
            WAPI.Response wapiResponse = new WAPI.Response();

            XMLActivity activity = new XMLActivity();
            activity.ProcessId = processId;
            activity.Name = activityName;

            String xmlActivity = activity.GetXML(false, 0);
            switch (operation)
            {
                case ActivityOperation.Acquire:
                    ActivityHelper.AcquireActivity(xmlActivity, user, ref wapiResponse);
                    break;

                case ActivityOperation.Assign:
                    ActivityHelper.AssignActivity(xmlActivity, user, ref wapiResponse);
                    break;

                case ActivityOperation.ReAssign:
                    ActivityHelper.ReassignWorkItem(xmlActivity, user, ref wapiResponse);
                    break;

                case ActivityOperation.Lock:
                    ActivityHelper.DoWorkItem(xmlActivity, user, ref wapiResponse);
                    break;

                case ActivityOperation.CompleteAsynch:
                    ActivityHelper.CompleteWorkItem(xmlActivity, user, resultCode, ref wapiResponse);
                    break;

                case ActivityOperation.Complete:
                    ActivityHelper.CompleteWorkItemSynch(xmlActivity, user, resultCode, ref wapiResponse);
                    break;

                case ActivityOperation.Stop:
                    ActivityHelper.StopWorkItem(xmlActivity, user, ref wapiResponse);
                    break;

                case ActivityOperation.Release:
                    ActivityHelper.ReleaseWorkItem(xmlActivity, ref wapiResponse);
                    break;

            }

            if  (wapiResponse.iCode != 0 )
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
        }

        #endregion
    }
}
